package org.dromara.system.controller;

import java.io.File;
import java.io.IOException;
import java.util.*;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.vo.SysOssUploadVo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.WbProductPatentVo;
import org.dromara.system.domain.bo.WbProductPatentBo;
import org.dromara.system.service.IWbProductPatentService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 产品专利
 *
 * @author Lion Li
 * @date 2024-03-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/productPatent")
public class WbProductPatentController extends BaseController {

    private final IWbProductPatentService wbProductPatentService;
    private final ISysOssService ossService;



    /**
     * 查询产品专利列表
     */
    @SaCheckPermission("system:productPatent:list")
    @GetMapping("/list")
    public TableDataInfo<WbProductPatentVo> list(WbProductPatentBo bo, PageQuery pageQuery) {
        return wbProductPatentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出产品专利列表
     */
    @SaCheckPermission("system:productPatent:export")
    @Log(title = "产品专利", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbProductPatentBo bo, HttpServletResponse response) {
        List<WbProductPatentVo> list = wbProductPatentService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品专利", WbProductPatentVo.class, response);
    }

    /**
     * 获取产品专利详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:productPatent:query")
    @GetMapping("/{id}")
    public R<WbProductPatentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wbProductPatentService.queryById(id));
    }

    /**
     * 新增产品专利
     */
    @SaCheckPermission("system:productPatent:add")
    @Log(title = "产品专利", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbProductPatentBo bo) {
        return toAjax(wbProductPatentService.insertByBo(bo));
    }

    /**
     * 修改产品专利
     */
    @SaCheckPermission("system:productPatent:edit")
    @Log(title = "产品专利", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbProductPatentBo bo) {
        return toAjax(wbProductPatentService.updateByBo(bo));
    }

    /**
     * 删除产品专利
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:productPatent:remove")
    @Log(title = "产品专利", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbProductPatentService.deleteWithValidByIds(List.of(ids), true));
    }

    @SaCheckPermission("system:productPatent:verify")
    @PostMapping("/verify")
    public R verify(@RequestParam("file") MultipartFile file) throws Exception {
        SysOssVo oss = ossService.upload(file);
        SysOssUploadVo uploadVo = new SysOssUploadVo();
        uploadVo.setUrl(oss.getUrl());
        uploadVo.setFileName(oss.getOriginalName());
        uploadVo.setOssId(oss.getOssId().toString());
        String verify ="";
        if (file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")).equals(".xls")&&!file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")).equals(".xlsx")){
            verify = wbProductPatentService.verify(file);
        }
        return R.ok(verify,uploadVo);
    }
}
